﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Psm.Util;
using Psm.PubSub.Messaging;

namespace Psm.PubSub.Serialization.Prefixed
{
    public class ByteBufferMessageSerializer
    {
        private ByteBuffer _buffer;

        public ByteBufferMessageSerializer(PubSubMessageType messageType, ByteBuffer buffer)
        {
            _buffer = buffer;
            _buffer.Write((byte)messageType);
        }

        public byte[] GetBytes()
        {
            return _buffer.GetBytes();
        }

        public bool AppendField(string item)
        {
            return _buffer.WritePrefixed(item);
        }

        public bool AppendTuple(int item1, ByteBuffer item2)
        {
            if (!_buffer.Write(item1)) return false;
            return _buffer.Write(item2);
        }

        public bool AppendTuple(ByteBuffer item1, ByteBuffer item2, ByteBuffer item3)
        {
            if (!_buffer.Write(item1)) return false;
            if (!_buffer.Write(item2)) return false;
            if (!_buffer.Write(item3)) return false;
            return true;
        }
    }
}
